//
//  HWBookView.m
//  嗨文
//
//  Created by 响当当 on 2019/1/9.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWBookView.h"
#import "HWRefreshHeader.h"
#import "HWBookCell.h"
#import "HWAdvertisementView.h"
#import "HWBookDetailViewController.h"

@interface HWBookView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tabelView;
@property (nonatomic, strong) HWAdvertisementView *advertisementView;
@property (nonatomic, strong)NSMutableArray *bookArr;
@property (nonatomic, assign)BOOL hasNextPage;
@property (nonatomic, strong)NSString *pageNum;
@end

@implementation HWBookView

- (NSMutableArray *)bookArr
{
    if (!_bookArr)
    {
//        _bookDic = @{@"bookTitle":@"修真界唯一锦鲤 连载中",@"bookAuthor":@"萧瑾瑜 阅读至23章",@"bookUpdateTime":@"5小时前 ·  第四百九十二章天王陵墓"};
        _bookArr = [[NSMutableArray alloc]init];
    }
    return _bookArr;
}

- (HWAdvertisementView *)advertisementView
{
    if (!_advertisementView)
    {
        _advertisementView = [[HWAdvertisementView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,74*kUIScaleBasePlus)];
    }
    return _advertisementView;
}

- (UITableView *)tabelView
{
    if (!_tabelView)
    {
        _tabelView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        _tabelView.separatorStyle = 0;
   
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpChapter)];
        
        // 设置文字
        [header setTitle:@"我的书架" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:12];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:10];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
        header.lastUpdatedTimeLabel.textColor = [UIColor clearColor];
        // 设置刷新控件
        self.tabelView.mj_header = header;
        
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"去添加更多喜欢的小说吧" forState:MJRefreshStateNoMoreData];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:14*kUIScaleBasePlus];
        
        // 设置颜色
        footer.stateLabel.textColor = JSALineColor;
        
        _tabelView.mj_footer = footer;
    }
    return _tabelView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.pageNum = @"1";
        self.hasNextPage = YES;
        [self addSubview:self.tabelView];
    }
    return self;
}

#pragma mark - tableView delegate & source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bookArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        return 74*kUIScaleBasePlus;
    }
    else
    {
        return 0.0f;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        return self.advertisementView;
    }
    else
    {
        return [UIView new];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWBookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
    if (!cell)
    {
        cell = [[HWBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeCell"];
        cell.selectionStyle = 0;
    }
    
    if([self.bookArr isValidArray])
    {
        HWBookShelfModel *model = [[HWBookShelfModel alloc]initWithDic:self.bookArr[indexPath.row]];
        [cell getBookDetail:model];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    HWBookDetailViewController *vc = [[HWBookDetailViewController alloc]init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
    HWBookShelfModel *model = [[HWBookShelfModel alloc]initWithDic:self.bookArr[indexPath.row]];
    [HWUtilties pushWebWithUrl:[NSString stringWithFormat:@"%@/detail?id=%@",WEBCOMME_HOST,[model.bookId stringValue]] title:model.bookName toUrl:@"/detail" toId:@"179a4f697cc89d36"];

}

#pragma mark - 下拉刷新
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        if (weakSelf.upLoadIndexBlcok)
        {
            if (self.hasNextPage)
            {
                NSInteger num = self.pageNum.integerValue + 1;
                self.upLoadIndexBlcok([NSString stringWithFormat:@"%ld",num],@"10");
                self.pageNum = [NSString stringWithFormat:@"%ld",num];
            }
            else
            {
                self.upLoadIndexBlcok(@"1",@"10");
            }
        }
        
        [weakSelf.tabelView.mj_footer endRefreshing];

    });
}

- (void)loadUpChapter
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (weakSelf.refreshBookShelfBlock)
        {
            [weakSelf.bookArr removeAllObjects];
            weakSelf.pageNum = @"1";
            weakSelf.hasNextPage = YES;
            weakSelf.tabelView.mj_footer.hidden = NO;
            weakSelf.refreshBookShelfBlock();
        }
        
        [weakSelf.tabelView.mj_header endRefreshing];
        
    });
}
- (void)getDate:(NSArray *)arr hasNetPage:(NSNumber *)hasNetPage
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage || self.bookArr.count < arr.count)
        {
            [self.bookArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNetPage boolValue];
            [self.tabelView reloadData];
        }
        else
        {
            self.tabelView.mj_footer.hidden = YES;
        }
        
    }

    
}

@end
